package com.ekuaitu.kuaitu.payment;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;

import com.alipay.sdk.app.PayTask;
import com.ekuaitu.kuaitu.R;
import com.ekuaitu.kuaitu.activity.InviteActivity;
import com.ekuaitu.kuaitu.bean.DepositBikeResultEvent;
import com.ekuaitu.kuaitu.bean.DepositCarResultEvent;
import com.ekuaitu.kuaitu.bean.MemberPayResultEvent;
import com.ekuaitu.kuaitu.utils.Constants;
import com.ekuaitu.kuaitu.utils.IntentKey;
import com.ekuaitu.kuaitu.utils.SharedPreferencesKey;
import com.ekuaitu.kuaitu.utils.SharedPreferencesUtils;
import com.ekuaitu.kuaitu.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.Map;


/**
 * Created by michal on 2016/10/11.
 */
public class AlipayUtils {


    private Activity activity;
    String orderInfo;
    private static final int SDK_PAY_FLAG = 1;
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            @SuppressWarnings("unchecked")
            AliPayResultBean payResult = new AliPayResultBean((Map<String, String>) msg.obj);
            String resultStatus = payResult.getResultStatus();
            switch (msg.what) {
                case SDK_PAY_FLAG:
                    int errCode;
                    if (resultStatus.equals("9000")) {
                        errCode = 0;
                        Log.i("aliPayResultCodeOk", payResult.getMemo() + "  &" + payResult.getResultStatus());
                    } else {
                        Log.i("aliPayResultCodeNo", payResult.getMemo() + "  &" + payResult.getResultStatus());
                        errCode = 1;
                    }
                    PayResult allPayResult = new PayResult();
                    allPayResult.handlePayResult(activity, 2,
                            SharedPreferencesUtils.getInstance(activity.getApplicationContext()).getIntValue(SharedPreferencesKey.USER_PAY_TYPE),
                            errCode);
                    break;
                default:
                    break;
            }
        }
    };

    public AlipayUtils(Activity activity) {
        this.activity = activity;
    }

    public void aliPay(final String orderInfo) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                PayTask alipay = new PayTask(activity);
                Map<String, String> result = alipay.payV2(orderInfo, true);
                Log.i("msp", result.toString());

                Message msg = new Message();
                msg.what = SDK_PAY_FLAG;
                msg.obj = result;
                mHandler.sendMessage(msg);
            }
        }).start();
    }


}
